﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LINQtoLDAPDemo_Model;

namespace LINQtoLDAPDemo_Winform.Views
{
    public partial class frmGroupMembers : Form
    {
        private Presenters.GroupMembersPresenter presenter;

        #region Constructors
        public frmGroupMembers()
        {
            InitializeComponent();
        }

        public frmGroupMembers(Models.NorthwindDAO _dao)
            : this()
        {
            presenter = new Presenters.GroupMembersPresenter(this, _dao);
        }
        #endregion

        #region Events
        private void cboGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (presenter != null)
                presenter.ChangeGroup();
        }

        private void btnAddToGroup_Click(object sender, EventArgs e)
        {
            Group group = (Group)cboGroups.SelectedItem;
            User[] users = lbUsers.SelectedItems.Cast<User>().ToArray();

            presenter.AddUsersToGroup(group, users);
        }

        private void btnRemoveFromGroup_Click(object sender, EventArgs e)
        {
            Group group = (Group)cboGroups.SelectedItem;
            User[] users = lbGroupMembers.SelectedItems.Cast<User>().ToArray();

            presenter.RemoveUsersFromGroup(group, users);
        }

        private void lnklblNextNonMemberPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            bool _incrementPageNumber = true, _isMember = false;

            presenter.ChangePageNumber(_incrementPageNumber, _isMember);
        }

        private void lnklblPreviousNonMemberPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            bool _incrementPageNumber = false, _isMember = false;

            presenter.ChangePageNumber(_incrementPageNumber, _isMember);
        }

        private void lnklblPreviousMemberPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            bool _incrementPageNumber = false, _isMember = true;

            presenter.ChangePageNumber(_incrementPageNumber, _isMember);
        }

        private void lnklblNextMemberPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            bool _incrementPageNumber = true, _isMember = true;

            presenter.ChangePageNumber(_incrementPageNumber, _isMember);
        }

        private void txtCurrentNonMemberPageNum_Leave(object sender, EventArgs e)
        {
            bool isMemberPage = false;
            ChangePageNumber(isMemberPage);
        }

        private void txtCurrentNonMemberPageNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            bool isMemberPage = false;
            SubmitPageNumberChangeIfEnterKeyIsPressed(e, isMemberPage);
        }

        private void txtCurrentMemberPageNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            bool isMemberPage = true;
            SubmitPageNumberChangeIfEnterKeyIsPressed(e, isMemberPage);
        }

        private void txtCurrentMemberPageNum_Leave(object sender, EventArgs e)
        {
            bool isMemberPage = true;
            ChangePageNumber(isMemberPage);
        }
        #endregion

        #region Data Binding methods
        public void SetGroupsBindingSource(Group[] groups)
        {
            groupBindingSource.DataSource = groups;
            cboGroups.DisplayMember = "Name";
        }

        public void SetNonMembersPageNumber(int pageNum)
        {
            txtCurrentNonMemberPageNum.Text = pageNum.ToString();
            txtCurrentNonMemberPageNum.Select(txtCurrentNonMemberPageNum.Text.Length, 0);
        }

        public void SetTotalNumberOfNonMemberPages(int pageTotal)
        {
            lblNonMemberPageCount.Text = pageTotal.ToString();
        }

        public void SetMembersPageNumber(int pageNum)
        {
            txtCurrentMemberPageNum.Text = pageNum.ToString();
            txtCurrentMemberPageNum.Select(txtCurrentMemberPageNum.Text.Length, 0);
        }

        public void SetTotalNumberOfMemberPages(int pageTotal)
        {
            lblMemberPageCount.Text = pageTotal.ToString();
        }

        public void SetNonMembersBindingSource(User[] users)
        {
            nonMemberBindingSource.DataSource = users;
            lbUsers.DisplayMember = "DisplayName";
        }

        public void SetMembersBindingSource(User[] users)
        {
            memberBindingSource.DataSource = users;
            lbGroupMembers.DisplayMember = "DisplayName";
        }

        public void SetPageLinksBindingSource(bool _isMember, bool _hasNextPage, bool _hasPreviousPage)
        {
            if (_isMember)
            {
                lnklblNextMemberPage.Enabled = _hasNextPage;
                lnklblPreviousMemberPage.Enabled = _hasPreviousPage;
            }
            else
            {
                lnklblNextNonMemberPage.Enabled = _hasNextPage;
                lnklblPreviousNonMemberPage.Enabled = _hasPreviousPage;
            }
        }
        #endregion

        public Group GetSelectedGroup()
        {
            return (Group)cboGroups.SelectedItem;
        }

        private void ChangePageNumber(bool _isMemberPage)
        {
            if (presenter != null)
            {
                string text = _isMemberPage ? txtCurrentMemberPageNum.Text : txtCurrentNonMemberPageNum.Text;
                
                presenter.ChangePageNumber(text, _isMemberPage);
            }
        }

        // TODO: Duplicate this functionality in WPF project
        private void SubmitPageNumberChangeIfEnterKeyIsPressed(KeyPressEventArgs _key, bool _isMemberPage)
        {
            if (_key.KeyChar == (char)Keys.Enter || _key.KeyChar == (char)Keys.Return)
            {
                ChangePageNumber(_isMemberPage);
                _key.Handled = true;
            }
        }
    }
}